﻿using Microsoft.Win32;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageHandler
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string _sourcePath;
        private SolidColorBrush _colorBrush0;
        private SolidColorBrush _colorBrush1_50;
        private SolidColorBrush _colorBrush51_100;
        private SolidColorBrush _colorBrush101_150;
        private SolidColorBrush _colorBrush151_200;
        private SolidColorBrush _colorBrush201_250;
        private SolidColorBrush _colorBrush251_255;

        public MainWindow()
        {
            InitializeComponent();
            InitializeData();
        }

        private void InitializeData()
        {
            _colorBrush0 = new SolidColorBrush(Colors.Black);
            _colorBrush1_50 = new SolidColorBrush(Colors.White);
            _colorBrush51_100 = new SolidColorBrush(Colors.White);
            _colorBrush101_150 = new SolidColorBrush(Colors.White);
            _colorBrush151_200 = new SolidColorBrush(Colors.White);
            _colorBrush201_250 = new SolidColorBrush(Colors.White);
            _colorBrush251_255 = new SolidColorBrush(Colors.White);
            BrushUpdated();
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "所有图片 (*.jpg*)|*.jpg";
            if (openFileDialog.ShowDialog() == true)
            {
                _sourcePath = openFileDialog.FileName;
                var bitmap = new BitmapImage(new Uri(_sourcePath, UriKind.RelativeOrAbsolute));
                imgSource.Source = bitmap;
                ToGrayImage();

            }
        }

        private void ToGrayImage()
        {
            var newPath = ImageHelper.ConvertToGrayImage(_sourcePath);
            Task.Factory.StartNew(async () =>
            {
                await Task.Delay(200);
                _ = Dispatcher.BeginInvoke(new Action(() =>
                {
                    imgGray.Source = new BitmapImage(new Uri(_sourcePath, UriKind.RelativeOrAbsolute));
                }));
            });
        }

        private void btn0_Click(object sender, RoutedEventArgs e)
        {
            PickerReplaceColor(ref _colorBrush0);
        }

        private void PickerReplaceColor(ref SolidColorBrush solidColorBrush)
        {
            var colorPickerWindow = new ColorPickerWindow();
            colorPickerWindow.ShowDialog();
            if (colorPickerWindow.DialogResult == true)
            {
                solidColorBrush = colorPickerWindow.SelectedBrush;
                BrushUpdated();
            }
        }
    
        private void BrushUpdated()
        {
            btn0.Content = _colorBrush0.ToString();
            btn0.Background = _colorBrush0.Clone();

            btn1_50.Content = _colorBrush1_50.ToString();
            btn1_50.Background = _colorBrush1_50;

            btn51_100.Content = _colorBrush51_100.ToString();
            btn51_100.Background = _colorBrush51_100;

            btn101_150.Content = _colorBrush101_150.ToString();
            btn101_150.Background = _colorBrush101_150;

            btn151_200.Content = _colorBrush151_200.ToString();
            btn151_200.Background = _colorBrush151_200;

            btn201_250.Content = _colorBrush201_250.ToString();
            btn201_250.Background = _colorBrush201_250;

            btn251_255.Content = _colorBrush251_255.ToString();
            btn251_255.Background = _colorBrush251_255;
        }

        private void btnReplaceColor_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}